package com.example.gslih.myapplication.file;

import android.app.Activity;
import android.content.Context;
import android.os.Environment;

import java.io.File;

import static android.os.Environment.MEDIA_MOUNTED;

/**
 * Create by Gene : 2018/12/19  14:50
 */
public class TtFileUtil {
    //当前包名  com.example.gslih.myapplication
    public static String getPackageName(Context context) {
        return context.getPackageName();
    }

    //当前类名  a.FilePathActivity
    public static String getLocalClassName(Activity activity) {
        return activity.getLocalClassName();
    }

    //内部File路径  /data/user/0/com.example.gslih.myapplication/files
    public static String getFilesDir(Context context) {
        return context.getFilesDir().toString();
    }

    //内部Cache路径  /data/user/0/com.example.gslih.myapplication/cache
    public static String getCacheDir(Context context) {
        return context.getCacheDir().toString();
    }

    //外部Cache路径  /storage/emulated/0/Android/data/com.example.gslih.myapplication/cache
    public static String getExternalCacheDir(Context context) {
        return context.getExternalCacheDir().toString();
    }

    //外部File路径  /storage/emulated/0/Android/data/com.example.gslih.myapplication/files
    public static String getExternalFilesDir(Context context) {
        return context.getExternalFilesDir("").toString();
    }

    //判断外部存储是否可用
    public static boolean ifExternalMounted() {
        return MEDIA_MOUNTED.equals(Environment.getExternalStorageState());
    }

    //内部存储根目录  /data
    public static String getDataDirectory() {
        return Environment.getDataDirectory().toString();
    }

    //外部存储根目录  /storage/emulated/0
    public static String getExternalStorageDirectory() {
        return Environment.getExternalStorageDirectory().toString();
    }

    //公路径  /storage/emulated/0
    public static String getExternalStoragePublicDirectory() {
        return Environment.getExternalStoragePublicDirectory("").toString();
    }

    //下载路径，实质任然是内部存储  /cache
    public static String getDownloadCacheDirectory() {
        return Environment.getDownloadCacheDirectory().toString();
    }

    //系统路径，实质任然是内部存储  /system
    public static String getRootDirectory() {
        return Environment.getRootDirectory().toString();
    }

    /********************************************************************************/

    /**
     * 判断外部存储是否可用
     */
    public static boolean isExternalStorageWritable() {
        String state = Environment.getExternalStorageState();
        if (Environment.MEDIA_MOUNTED.equals(state)) {
            return true;
        }
        return false;
    }

    /**
     * 创建文件夹
     * @param dirPath 存储路径
     * @param path  文件路径
     * @return
     */
    public static String makeDirs(String dirPath,String path) {
        String[] dirs = path.replace(dirPath, "").split("/");
        StringBuffer filePath = new StringBuffer(dirPath);
        for (String dir : dirs) {
            if (!"".equals(dir) && !dir.equals(dirPath)) {
                filePath.append("/").append(dir);
                File destDir = new File(filePath.toString());
                if (!destDir.exists()) {
                    boolean b = destDir.mkdirs();
                    if (!b) {
                        return null;
                    }
                }
            }
        }
        return filePath.toString();
    }

}
